package com.wolfalpha.lwtool.activity;

import com.wolfalpha.lwtool.R;
import com.wolfalpha.lwtool.service.impl.UserServiceImpl;
import com.wolfalpha.lwtool.vo.LWJob;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MyJobInfoActivity extends Activity implements OnClickListener {

	private UserServiceImpl usv;
	private LWJob job;
	private Button del;
	private Button stop;
	private TextView json;
	private ProgressDialog pd;
	private JobTask jobTask = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mjbinfo);
		Bundle bundle = getIntent().getExtras();
		if (bundle.containsKey("job")) {
			job = (LWJob) bundle.getSerializable("job");
		} else {
			Toast.makeText(this, "数据错误", Toast.LENGTH_SHORT).show();
			finish();
		}
		usv = new UserServiceImpl();

		initView();
		initListeners();
	}

	private void initView() {
		del = (Button) findViewById(R.id.delJob);
		stop = (Button) findViewById(R.id.stopJob);
		json = (TextView) findViewById(R.id.job_json);
		String jf = job.toString();
		jf = jf.replace(",", "\n");
		jf = jf.replace("[", "\n\n ");
		jf = jf.replace("]", "\n");
		jf = jf.replace("=", "=\t");
		json.setText(jf);
	}

	private void initListeners() {
		del.setOnClickListener(this);
		stop.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		String params[] = new String[] { "1", String.valueOf(job.getId()) };
		switch (arg0.getId()) {
		case R.id.delJob:
			jobTask = new JobTask();
			params[0] = "1";
			jobTask.execute(params);
			break;
		case R.id.stopJob:
			jobTask = new JobTask();
			params[0] = "2";
			jobTask.execute(params);
			break;
		}
	}

	private class JobTask extends AsyncTask<String, Integer, String> {
		// onPreExecute方法用于在执行后台任务前做一些UI操作
		@Override
		protected void onPreExecute() {
			if (pd == null)
				pd = new ProgressDialog(MyJobInfoActivity.this);
			pd.setMessage("正在请求数据");
			pd.setCancelable(false);
			pd.show();
		}

		// doInBackground方法内部执行后台任务,不可在此方法内修改UI
		@Override
		protected String doInBackground(String... params) {
			try {
				usv.modifyMyJob(Integer.parseInt(params[0]),
						Integer.parseInt(params[1]));
			} catch (Exception e) {
				e.printStackTrace();
				this.cancel(true);
			}
			return "";
		}

		// onProgressUpdate方法用于更新进度信息
		@Override
		protected void onProgressUpdate(Integer... progresses) {
		}

		// onPostExecute方法用于在执行完后台任务后更新UI,显示结果
		@Override
		protected void onPostExecute(String result) {
			Toast.makeText(MyJobInfoActivity.this, "操作成功", Toast.LENGTH_SHORT)
					.show();
			pd.dismiss();
			MyJobInfoActivity.this.finish();
		}

		// onCancelled方法用于在取消执行中的任务时更改UI
		@Override
		protected void onCancelled() {
			Toast.makeText(MyJobInfoActivity.this, "获取失败，请检查网络",
					Toast.LENGTH_SHORT).show();
			pd.dismiss();
		}
	}
}
